import yizhi_html_escape_1 from "yizhi-html-escape";
import {parseXML} from './xml'
import { parseLyricTag  } from "./lyricCommon.js"

export const parse = (qrcText: string)=>{
    //解析XML数据，得到歌词数据
    const lyric = (() => {
        try {
            //const res = (0, xml_1.parseXML)(qrcText).root;
            const res =  parseXML(qrcText).root;
            //QrcInfos标签
            if (res.name.toLowerCase() != 'QrcInfos'.toLowerCase())
                throw new Error('match QrcInfos TAG failed');
            if (!(res.children instanceof Array))
                throw new Error('match QrcInfos TAG failed');
            //LyricInfo标签
            const lyricInfoTag = res.children.find(e => e.name.toLowerCase() === 'LyricInfo'.toLowerCase());
            if (!lyricInfoTag)
                throw new Error(`match LyricInfo TAG failed`);
            if (!(lyricInfoTag.children instanceof Array))
                throw new Error(`match LyricInfo TAG failed`);
            //歌词列表
            const lyricTags = lyricInfoTag.children.filter(t => /^Lyric_\d+/.test(t.name));
            return lyricTags.map(t => {
                const lyric = { type: 1, content: '' };
                t.props.forEach(p => {
                    if (p.name.toLowerCase() == 'LyricContent'.toLowerCase())
                        lyric.content = p.value ?? '';
                    else if (p.name.toLowerCase() == 'LyricType')
                        lyric.type = parseInt(p.value ?? '1');
                });
                return lyric;
            }).filter(l => l.content);
        }
        catch (err) {
            throw new Error(`parse QRC XML data failed`);
        }
    })()[0];
    if (!lyric)
        throw new Error(`QRC file not contains any lyric data`);
    //结果
    const result = { type: "KARA", content: [] };
    //逐行处理
    lyric.content.split(/\r?\n/).forEach(line => {
        line = line.trim();
        //歌词行
        const match = line.match(/^\[\s*(\d+)\s*,\s*(\d+)\s*\](.*)$/);
        if (match) {
            //目标信息
            const lrcLine = {
                start: parseInt(match[1]),
                duration: parseInt(match[2]),
                content: [],
            };
            //一直往后所搜词语
            let rem = match[3];
            while (rem.length) {
                const match = rem.match(/\(\s*(\d+)\s*,\s*(\d+)\s*\)/);
                if (match) {
                    const word = {
                        start: parseInt(match[1]) - lrcLine.start,
                        duration: parseInt(match[2]),
                        content: (0, yizhi_html_escape_1.unescape)(rem.substring(0, match.index)),
                    };
                    if (word.content)
                        lrcLine.content.push(word);
                    rem = rem.substring(match.index + match[0].length);
                }
            }
            //保存行
            if (lrcLine.content.length)
                result.content.push(lrcLine);
        }
        //标签
        else
            return parseLyricTag(result, line);
    });
    return result;
}

